/*
 * cm_time.h
 *
 *  Created on: 2014年5月14日
 *      Author: chens
 */

#ifndef CM_TIME_H_
#define CM_TIME_H_

#include <string.h>
#include <time.h>
#include <sys/time.h>

struct cm_time {
	struct timeval unix_time;
	struct tm c_time;
};

static inline int cm_gettime(struct cm_time *t, const struct timeval *value)
{
	t->unix_time = *value;
	struct tm *temp = localtime(&(value->tv_sec));

	if(temp == NULL)
		return -1;

	t->c_time = *(temp);

	return 0;
}

static inline const char *timeval2str(const struct timeval time)
{
	static char str[64];
	struct tm *t = localtime(&(time.tv_sec));
	memset(str, 0, 64);
	sprintf(str, "%d_%d_%d_%d_%d_%d_%d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
			t->tm_hour, t->tm_min, t->tm_sec, time.tv_usec);
	return str;

}

#endif /* CM_TIME_H_ */
